﻿using IMark.DAO;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IMark.Admin.Product
{
    public partial class Add : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Utils.CheckAuthentication())
            {
                if (!IsPostBack)
                {
                    LoadData();
                }
            }
        }
        private void LoadData()
        {
            info.InnerHtml = string.Empty;
            if ((string)Session["Info"] != "")
            {
                info.InnerHtml = (string)Session["Info"];
                Session["Info"] = "";
            }
            ListTypeProduct();
        }        
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (txtProductName.Value + txtContent.Value != "")
            {
                SAN_PHAM sanPham = new SAN_PHAM();                
                sanPham.DacBiet = 1;
                sanPham.DonGia = long.Parse(txtPrice.Value.Replace(",",""));
                sanPham.GiamGia = 0;
                sanPham.IDLoaiSanPham = long.Parse(Request.Form["productType"]);
                sanPham.MoTaChiTiet = txtContent.Value;
                sanPham.MoTaTomTat = txtSummary.Value;
                sanPham.MetaDescription = txtMetaDescription.Value;
                sanPham.MetaKeyword = txtMetaKeyword.Value;
                sanPham.MetaTitle = txtMetaTitle.Value;
                sanPham.NgayBatDau = null;
                sanPham.NgayKetThuc = null;
                sanPham.NgaySua = DateTime.Now;
                sanPham.NgayThem = DateTime.Now;
                sanPham.SoLuong = int.Parse(txtQuantity.Value);
                sanPham.TenSanPham = txtProductName.Value;
                sanPham.TrangThai = 1;
                sanPham.Xoa = 0;
                long status = SanPhamModels.Add(sanPham);
                if (status >0)
                {
                    List<HINH_SAN_PHAM> lstImages = new List<HINH_SAN_PHAM>();
                    if(fUpload.HasFiles)
                    {
                        foreach (HttpPostedFile uploadedFile in fUpload.PostedFiles)
                        {                            
                            string fileNew = Guid.NewGuid().ToString()  + Path.GetExtension(uploadedFile.FileName);
                            uploadedFile.SaveAs(System.IO.Path.Combine(Server.MapPath("~/images/products/"),fileNew ));
                            lstImages.Add(new HINH_SAN_PHAM() { Hinh = fileNew, IDSanPham = sanPham.ID });
                        }
                        HinhSanPhamModels.Add(lstImages);
                    }
                    Session["Info"] = "<div class='alert alert-success'>Thêm sản phẩm thành công !!!</div>";
                    Response.Redirect("~/Admin/Product/Add.aspx");
                }
                else
                {
                    info.InnerHtml = "<div class='alert alert-danger'>Đã xảy ra lỗi, vui lòng thử lại !!!</div>";
                }
            }
            else
            {
                info.InnerHtml = "<div class='alert alert-danger'>Vui lòng nhập đủ thông tin sản phẩm !!!</div>";
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Admin/Product/Index.aspx");
        }

        private void ListTypeProduct()
        {
            lstProductType.InnerHtml = LoaiSanPhamModels.ShowSelectProductType(true);
        }
    }
}